<%--
  Created by IntelliJ IDEA.
  User: lijincheng
  Date: 2020/7/11
  Time: 9:56 下午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/views/common/head.jsp"%>

    <div class="right">
        <div class="location">
            <strong>你现在所在的位置是:</strong>
            <span>订单管理页面</span>
        </div>
        <div class="search">
            <form method="post" >
                <span>商品名称：</span>
                <input name="queryProductName" id="productName" type="text" value="">
                <span>供应商：</span>
                <select id="select" name="queryProviderId">

                    <option value="">--请选择--</option>

                </select>
                <span>是否付款：</span>
                <select id="pay" name="queryIsPayment">
                    <option value="">--请选择--</option>
                    <option value="1">未付款</option>
                    <option value="2">已付款</option>
                </select>
                <input type="hidden" name="pageIndex" value="1"/>
                <input value="查 询" type="button" onclick="search()" id="searchbutton">
                <a href="/bill/add">添加订单</a>
            </form>
        </div>
        <!--账单表格 样式和供应商公用-->
        <div style="height: 75%">
            <table class="providerTable" cellpadding="0" id="table" cellspacing="0">
                <tr class="firstTr">
                    <th width="10%">订单编码</th>
                    <th width="20%">商品名称</th>
                    <th width="10%">供应商</th>
                    <th width="10%">订单金额</th>
                    <th width="10%">是否付款</th>
                    <th width="10%">创建时间</th>
                    <th width="30%">操作</th>
                </tr>
            </table>

        </div>

        <div class="page-bar" id="page-bar">
            <ul class="page-num-ul clearfix">
                <li>共<span id="total"></span>条记录&nbsp;&nbsp; <span id="curr"></span>/<span id="pages"></span>页&nbsp;
                </li>

                <a onclick="pre()">上一页</a>
                <a onclick="next()">下一页</a>
                <a onclick="lastPage()">最后一页</a>

                &nbsp;&nbsp;
            </ul>
            <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage" class="page-key"/>页
	     <button type="button" class="page-btn"
                 onClick="">GO</button>
		</span>
        </div>
    </div>
</section>

<!--点击删除按钮后弹出的页面-->
<div class="zhezhao"></div>
<div class="remove" id="removeBi">
    <div class="removerChid">
        <h2>提示</h2>
        <div class="removeMain">
            <p>你确定要删除该订单吗？</p>
            <a href="#" id="yes">确定</a>
            <a href="#" id="no">取消</a>
        </div>
    </div>
</div>


<footer class="footer">
    千峰科技
</footer>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/time.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/common.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/calendar/WdatePicker.js"></script>
<script>
    $.ajax({
        async:"true",
        type:"GET",
        url:"/provider/getList",
        success:function (data) {
            if(data!=null){
                console.log(data)
                for (let i = 0; i < data.length; i++) {
                    var c = '<option value="'+data[i].id+'">'+data[i].proname+'</option>';
                    $("#select").append(c);
                }
            }
        }
    });
</script>
<script>
    $.ajax({
        async: true,
        type: "GET",
        data: {"curr": 1,"goodsName":$("#productName").val(),"provider":$("#select").val(),"payStaute":$("#pay").val()},
        url: "/bill/getBillList",
        success: function (data) {
            console.log(data);
            console.log(data.length)
            if (data != null) {
                $('#table tr:gt(0)').remove();
                let r = data
                for (let i = 0; i < r.length; i++) {
                    var c = '<tr>\n' +
                        '\t<td>\n' +
                        '\t\t<span>' + r[i].billCode + '</span>\n' +
                        '\t</td>\n' +
                        '\t<td>\n' +
                        '\t\t<span>' + r[i].productName + '</span>\n' +
                        '\t</td>\n' +
                        '\t<td>\n' +
                        '\t\t<span>' + r[i].proName + '</span>\n' +
                        '\t</td>\n' +
                        '\t<td>\n' +
                        '\t\t<span>' + r[i].totalPrice + '</span>\n' +
                        '\t</td>\n' +
                        '\t<td>\n' +
                        '\t\t<span>\n';
                    if (r[i].isPayment == 1) {
                        c += '未支付';
                    } else if (r[i].isPayment == 2) {
                        c += '已支付';
                    }
                    c += '</span>\n' +
                        '\t</td>\n' +
                        '\t<td>\n' +
                        '\t\t<span>\n' +
                        '\t\t\t' + formatDate(new Date(r[i].creationDate))   + '\n' +
                        '\t\t</span>\n' +
                        '\t</td>\n' +
                        '\t<td>\n' +
                        '\t\t<span><a class="viewBill" href="/bill/viewBill?billid=' + r[i].id + '" billcc=' + r[i].billCode + ' ><img src="/statics/images/read.png" alt="查看"\n' +
                        '\t\t\t\t title="查看" /></a></span>\n' +
                        '\t\t<span><a class="modifyBill" href="/bill/modify?billid=' + r[i].id + '" billcc=' + r[i].billCode + '><img src="/statics/images/xiugai.png"\n' +
                        '\t\t\t\t alt="修改" title="修改" /></a></span>\n' +
                        '\t\t<span><a class="deleteBill" href="/bill/delete?billid=' + r[i].id + '" billcc=' + r[i].billCode + '><img src="/statics/images/schu.png" alt="删除"\n' +
                        '\t\t\t\t title="删除" onclick="return delete_bill()"/></a></span>\n' +
                        '\t</td>\n' +
                        '</tr>'
                    $("#table").append(c);
                }
                $.ajax({
                    async: true,
                    type: "GET",
                    data: {"curr": 1,"goodsName":$("#productName").val(),"provider":$("#select").val(),"payStaute":$("#pay").val()},
                    url: "/bill/getBillPage",
                    success: function (pageInfo) {
                        $("#total").text(pageInfo.totals);
                        $("#curr").text(pageInfo.pageNum);
                        $("#pages").text(pageInfo.pagess)
                    }
                });
            }
        }
    });

    function next() {
        var curr = parseInt($("#curr").text());
        if (curr + 1 > parseInt($("#pages").text())) {
            alert("最后一页");
        } else {
            getData(curr+1);
        }
    }

    function pre() {
        var curr = parseInt($("#curr").text());
        if (curr - 1 == 0) {
            alert("第一页");
        } else {
            getData(curr-1);
        }
    }

    function lastPage() {
        getData(parseInt($("#pages").text()));
    }

    function jumpPage() {
        var curr = parseInt($("#inputPage").val());
        if (curr<=0 || curr >parseInt($("#pages").text())){
            alert("请输入正确的页数");
        }else {
            getData(curr);
        }
    }

    function getData(curr) {
        $.ajax({
            async: true,
            type: "POST",
            data: {"curr": curr,"goodsName":$("#productName").val(),"provider":$("#select").val(),"payStaute":$("#pay").val()},
            url: "/bill/getBillPage",
            success: function (pageInfo) {
                if (pageInfo != null) {
                    $("#curr").text(pageInfo.pageNum);
                    $.ajax({
                        async: true,
                        type: "POST",
                        data: {"curr": curr,"goodsName":$("#productName").val(),"provider":$("#select").val(),"payStaute":$("#pay").val()},
                        url: "/bill/getBillList",
                        success: function (data) {
                            if (data != null) {
                                $('#table tr:gt(0)').remove();
                                let r = data;
                                for (let i = 0; i < r.length; i++) {
                                    var c = '<tr>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span>' + r[i].billCode + '</span>\n' +
                                        '\t</td>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span>' + r[i].productName + '</span>\n' +
                                        '\t</td>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span>' + r[i].proName + '</span>\n' +
                                        '\t</td>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span>' + r[i].totalPrice + '</span>\n' +
                                        '\t</td>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span>\n';
                                    if (r[i].isPayment == 1) {
                                        c += '未支付';
                                    } else if (r[i].isPayment == 2) {
                                        c += '已支付';
                                    }
                                    c += '</span>\n' +
                                        '\t</td>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span>\n' +
                                        '\t\t\t' + formatDate(new Date(r[i].creationDate)) + '\n' +
                                        '\t\t</span>\n' +
                                        '\t</td>\n' +
                                        '\t<td>\n' +
                                        '\t\t<span><a class="viewBill" href="/bill/viewBill?billid=' + r[i].id + '" billcc=' + r[i].billCode + ' ><img src="/statics/images/read.png" alt="查看"\n' +
                                        '\t\t\t\t title="查看" /></a></span>\n' +
                                        '\t\t<span><a class="modifyBill" href="/bill/modify?billid=' + r[i].id + '" billcc=' + r[i].billCode + '><img src="/statics/images/xiugai.png"\n' +
                                        '\t\t\t\t alt="修改" title="修改" /></a></span>\n' +
                                        '\t\t<span><a class="deleteBill" href="/bill/delete?billid=' + r[i].id + '" billcc=' + r[i].billCode + '><img src="/statics/images/schu.png" alt="删除"\n' +
                                        '\t\t\t\t title="删除" onclick="return delete_bill()"/></a></span>\n' +
                                        '\t</td>\n' +
                                        '</tr>';
                                    $("#table").append(c);
                                }
                            }
                        }
                    });
                }
            }
        });
    }

    function search() {
        $.ajax({
            async: true,
            type: "GET",
            data: {"curr": 1,"goodsName":$("#productName").val(),"provider":$("#select").val(),"payStaute":$("#pay").val()},
            url: "/bill/getBillPage",
            success: function (pageInfo) {
                $("#total").text(pageInfo.totals);
                $("#curr").text(pageInfo.pageNum);
                $("#pages").text(pageInfo.pagess)
            }
        });
        getData(1);
    }

    function delete_bill() {
        if (parseInt(${user.getUserrole()}) != 3) {
            var msg = "您真的确定要删除吗？\n\n请确认！";
            if (confirm(msg) == true) {
                return true;
            } else {
                return false;
            }
        } else {
            alert("权限不足");
            return false;
        }

    }

    function formatDate(now) {
        var year=now.getFullYear();  //取得4位数的年份
        var month=now.getMonth()+1;  //取得日期中的月份，其中0表示1月，11表示12月
        var date=now.getDate();      //返回日期月份中的天数（1到31）
        var hour=now.getHours();     //返回日期中的小时数（0到23）
        var minute=now.getMinutes(); //返回日期中的分钟数（0到59）
        var second=now.getSeconds(); //返回日期中的秒数（0到59）
        return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
    }

</script>
<script>

</script>
</body>
</html>
